package com.cf.controller;

import com.cf.entity.Address;
import com.cf.entity.Result;
import com.cf.entity.User;
import com.cf.service.AddressService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class AddressController  {
    @Autowired
    private AddressService addressService;

    //根据用户id查询所有用户地址
    @RequestMapping("/selectUserAddress")
    public Object selectUserAddress( int pagenum,int pagesize,long userid){
        PageInfo<Address> page = addressService.selectUserAddress(pagenum,pagesize,userid);
        if (page.getList().size() != 0){
            return new Result(200,"查询成功",page);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

    //根据addressid查询用户地址
    @RequestMapping("/selectByAddressid")
    public Object selectByAddressid(long addressid,long userid){
        List<Address> addresses = addressService.selectAllAddress(userid);
        Address ad = addressService.selectByAddressid(addressid);
        Result result = null;
        for(Address address : addresses){
            if (ad.getAddressid() == address.getAddressid()) {
                result = new Result(200,"查询成功",address);
                break;
            }else {
                result = new Result(400,"查询失败",null);
            }
        }
        return result;
    }
//    根据地址编号查询地址
    @RequestMapping("/selectAddressByid")
    public Object selectAddressByid(long addressid){
        Address address = addressService.selectByAddressid(addressid);
        if(address!=null){
            return new Result(200,"查询成功",address);
        }else {
            return new Result(400,"查询失败",null);
        }

    }

    //修改用户地址
    @RequestMapping("/updateAddress")
    public Object updateAddress(Address address){
        int i = addressService.updateAddress(address);
        if(i != 0){
            return new Result(200,"修改成功",null);
        }else {
            return new Result(400,"修改失败",null);
        }
    }
    //删除地址信息
    @RequestMapping("/deleteAddress")
    public Object deleteAddress(long addressid){
        int i = addressService.deleteAddress(addressid);
        if(i != 0){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }
    //添加地址
    @RequestMapping("/addAddress")
    public Object addAddress(Address address){
        int i = addressService.addAddress(address);
        if(i != 0){
            return new Result(200,"添加成功",null);
        }else {
            return new Result(400,"添加失败",null);
        }
    }
    @RequestMapping("/selectAlladdress")
    public Object selectAlladdress(long userid){
        List<Address> page = addressService.selectAllAddress(userid);
        if (page.size() != 0){
            return new Result(200,"查询成功",page);
        }else {
            return new Result(400,"查询失败",null);
        }
    }

}
